<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/1/27
 * Time: 10:05
 */
namespace Home\Controller;
use Think\Model;
use Overtrue\Wechat\Js;
use Think\Page;

class VoteController extends IndexController
{
    public function index(){

        $openid = session('openid');
        if(empty($openid)){
           $this->openid();
        }
        //查询战队
        $Corps = D('Corps');
        $data1 = $Corps->where("status=1")->field("corpsnumber,corpsname")->select();
        $this->assign("data1",$data1);
        $wechat = C('wechat');
        $jsapi = new Js($wechat['app'], $wechat['secret'] );
        $api = array('onMenuShareTimeline', 'onMenuShareAppMessage','onMenuShareQZone','onMenuShareQQ','onMenuShareWeibo');
        $this->assign('js',$jsapi->config($api,false));
        $this->display();
    }

    public function openid()
    {
        if(empty($_GET['openid'])) {
            $auth_callback = 'http://'.$_SERVER['HTTP_HOST'].U('index');
            $auth_url = 'http://wxoauth.xuanzhen-tech.com/wx/auth?callback=' . urlencode($auth_callback);
            redirect($auth_url);
        }
        $openid = $_GET['openid'];
        $User = D('User');
        $data =  $User->where("openid='$openid'")->find();
        if(empty($data)){
            $userinfo = \httpGetJSON("http://wxoauth.xuanzhen-tech.com/wx/users/".$openid);
            $user = array(
                'openid'=>$openid,
                'oauthdata' => json_encode($userinfo),
            );
            $User->add($user);
        }
        session("openid",$openid);
        return $openid;

    }

    public function initUser(){
        $Members = D('Members');
        $where = 'true';
        $coprs = $_GET['corps'];
        if(!empty($coprs)){
            $where="lolnumbers = '$coprs'";
        }
        $weizhi = $_GET['weizhi'];
        if(!empty($weizhi)){
            $where.=" and game_position like '%".$weizhi."%'";
        }
        $sex  =$_GET['sex'];
        if(!empty($sex)){
            if($sex=='未知'){
                $where.=" and sex !='男' and sex!='女'";
            }
            else{
                $where.=" and sex = '$sex'";
            }
        }
        $count = $Members->where($where)->count();
        $page = new Page($count,10);
        $data = $Members->join("left join lol_corps on lol_members.lolnumbers = lol_corps.corpsnumber and status=1")
            ->where($where)->limit($page->firstRow,$page->listRows)->field("lol_members.*,corpsname")->order("votes_num desc")->select();
        $openid = session("openid");
        $Vote  = D('Vote');
        $now_date = date('Y-m-d');
        for($i=0;$i<count($data);$i++){
            $is_vote = $Vote->where("openid='$openid' and user_id='".$data[$i]['id'] ."' and created_date='$now_date'")->find()?1:0;
            $data[$i]['is_vote'] = $is_vote;
        }
        $totalPage = ($count + 10-1) / 10;
        $num = $count - (int)$_GET['p']*10;
        $num = $num>0?$num:0;
        $this->ajaxReturn(array(
            'data' =>$data,
            'totalPage'=>floor($totalPage),
            'num' =>$num
        ));
    }
    public function is_vote(){
        $openid = session('openid');
        $Vote  = D('Vote');
        $now_date = date('Y-m-d');
        $Vote->where("openid='$openid' and  created_date='$now_date'")->find()?$this->error():$this->success();
    }
    public function do_vote($id){
        $openid = session('openid');
        $Vote  = D('Vote');
        if(empty($openid)){
            $this->error("投票失败,未登录");
        }
        $now_date = date('Y-m-d');
        $is_vote = $Vote->where("openid='$openid' and created_date='$now_date'")->find()?1:0;
        if($is_vote==1){
            $this->error("投票失败,每天只能投一次");
        }
        $Vote->add(array(
            'openid'=>$openid,
            'created_date' =>$now_date,
            'user_id'=>$id
        ));
        D('Members')->where("id=$id")->setInc('votes_num',1);

        $data  =  D('Members')->where("id=$id")->field("lolnumbers")->find();
        D('Corps')->where("corpsnumber='".$data['lolnumbers']."'")->setInc("votes_num",1);
        $this->success("投票成功");
    }
    public function corps($corpsnumber){
        $Corps  = D('Corps');

        $data = $Corps->where("corpsnumber='$corpsnumber'")->find();
        $ranking  = $Corps->where("votes_num>".$data['votes_num'])->count();
        $data['ranking']= $ranking+1;
        $this->ajaxReturn($data);
    }
}